package com.nuanshui.heatedloan.controller.display.credit;


import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.nuanshui.framework.lang.SysStringUtils;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.controller.BaseController;
import com.nuanshui.heatedloan.credit.facade.CustomerPeopleRelationshipService;
import com.nuanshui.heatedloan.credit.form.CustomerPeopleRelationshipForm;
import com.nuanshui.heatedloan.credit.vo.CustomerPeopleRelationshipVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangxw on 2017/6/27.
 */


@RequestMapping("credit/peopleRelationship")
@RestController
public class CustomerPeopleRelationshipController extends BaseController {

    @Reference(url= GlobalParam.CREDIT_DUBBO_SERVER,timeout=GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.CREDIT_DUBBO_SERVER_VERSION)
    private CustomerPeopleRelationshipService customerPeopleRelationshipService;

    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public String addCustomerPeopleRelationshipInfo(String  customerPeopleRelationshipMsg){
        if(SysStringUtils.isEmpty(customerPeopleRelationshipMsg)) return error("请设置相关信息");
        List<CustomerPeopleRelationshipForm>  customerPeopleRelationshipForms=
                JSON.parseArray(customerPeopleRelationshipMsg,CustomerPeopleRelationshipForm.class);
        String userId= getCurrentUser().getId();
        for(CustomerPeopleRelationshipForm customerPeopleRelationshipForm:customerPeopleRelationshipForms){
            customerPeopleRelationshipForm.setUserId(userId);
            customerPeopleRelationshipForm.setStatus("1");
            if(!SysStringUtils.isEmpty(customerPeopleRelationshipForm.getId())){
                customerPeopleRelationshipService.updateCustomerPeopleRelationship(customerPeopleRelationshipForm);
            }else{
                customerPeopleRelationshipService.saveCustomerPeopleRelationship(customerPeopleRelationshipForm);
            }
        }
        return success(customerPeopleRelationshipForms);
    }


    @RequestMapping(value = "/deleteById",method = RequestMethod.POST)
    public String deleteCustomerPeopleRelationshipInfo(String id){
        customerPeopleRelationshipService.deleteCustomerPeopleRelationship(id);
        return success();
    }

    @RequestMapping(value = "getInfoById",method = RequestMethod.POST)
    public String getCustomerPeopleRelationshipInfo(String id){
        CustomerPeopleRelationshipVO customerPeopleRelationshipVO=new CustomerPeopleRelationshipVO();
        customerPeopleRelationshipVO= customerPeopleRelationshipService.loadVO(id);
        return success(customerPeopleRelationshipVO);

    }

    @RequestMapping(value = "getInfoByCustomId",method = RequestMethod.POST)
   public String getCustomerPeopleRelationshipByCustomId(){
        List<CustomerPeopleRelationshipVO> lists=new ArrayList<CustomerPeopleRelationshipVO>();
        String userId= getCurrentUser().getId();
       lists=customerPeopleRelationshipService.loadcustomerPeopleRelationshipVOByCusId(userId);
       return success(lists);
   }
}
